//
//  ShopCartViewController.m
//  EasyToTesco
//
//  Created by mc on 16/1/31.
//  Copyright © 2016年 Jack. All rights reserved.
//   购物车视图控制器

#import "ShopCartViewController.h"
#import "HeaderFileForEasyToTesco.h"
#import "ShopCartTableViewCell.h"

#import "ShopCartToolbar.h"
#import "SCProductInfo.h"
#import "TotalPriceView.h"

#import "ShopCartEditTableViewCell.h"

#import "ShoperDetailViewController.h"

#import "ReadLoginData.h"

//自定义的猜你喜欢视图
#import "GuessLoveView.h"
//结算
#import "OrderDetailViewController.h"

#import "AFNetworking.h"
#import "SDRefresh.h"

#import "SVProgressHUD.h"

#import "NSNull+NullCast.h"


@interface ShopCartViewController ()<UITableViewDataSource, UITableViewDelegate, ShopCartTableViewCellDelegate, ShopCartToolbarDelegate, UIAlertViewDelegate, ShopCartEditTableViewCellDelegate>
{
    UILabel *messagelabel;
    UIButton *messageBtn;
    
    //总价
    float allPrice;
    //设置全选按钮的bool
    BOOL Alljudge;
}
//创建tableview属性
@property (nonatomic, strong)UITableView *ShopCartTableview;


//添加下方的工具栏
@property (nonatomic, strong)ShopCartToolbar *toolBar;


//购物车中的商品信息
@property (nonatomic,strong)NSMutableArray *ProductsArray;

@property (nonatomic, strong) NSMutableDictionary *dicM;

@property (nonatomic, strong) SCProductInfo *scpInfo;

// 购物车中商品的被选中状态(NSString:@"YES"/@"NO")
@property (nonatomic, strong) NSMutableArray *cellItems;
// 是否处于全选状态
@property (nonatomic, assign, getter=isAllSelected) BOOL selectAll;
// 是否正在编辑购物车
@property (nonatomic, assign) BOOL isEditing;

@property (nonatomic, strong) NSMutableArray *allGoodsArrM;
@property (nonatomic, assign) float allGoodsPrice;

//刷新控件
@property (nonatomic, weak) SDRefreshFooterView *refreshFooter;
@property (nonatomic, weak) SDRefreshHeaderView *refreshHeader;


@property(nonatomic,strong) NSMutableArray *wholeAddArray;

@end

@implementation ShopCartViewController

- (void)loadView {
    [super loadView];
    [SVProgressHUD show];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [SVProgressHUD dismiss];
}

- (NSMutableArray *)ProductsArray {
    
    if (!_ProductsArray) {
        _ProductsArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _ProductsArray;
}

- (NSMutableArray *)allGoodsArrM {
    
    if (!_allGoodsArrM) {
        _allGoodsArrM = [[NSMutableArray alloc] init];
    }
    return _allGoodsArrM;
}

- (NSMutableDictionary *)dicM {
    
    if (!_dicM) {
        _dicM = [NSMutableDictionary dictionary];
    }
    return _dicM;
}

- (NSMutableArray *)wholeAddArray
{
    if (!_wholeAddArray) {
        _wholeAddArray = [[NSMutableArray alloc] init];
    }
    return _wholeAddArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    Alljudge = YES;
    
    [self addShopCartUI];
    
//    [self addgetaddressdatasources];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupshopCartHeader) name:@"refresh" object:nil];
    [self setupshopCartHeader];
    

}



#pragma mark - 添加上下拉刷新
- (void)setupshopCartHeader{
    //添加下拉刷新对象
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    
    //     默认是在navigationController环境下，如果不是在此环境下，请设置
    refreshHeader.isEffectedByNavigationController = NO;
    
    //加入到目标tableview上
    [refreshHeader addToScrollView:self.ShopCartTableview];
    
    //添加下拉加载内容的方法
    __weak SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    __weak typeof(self) weakSelf = self;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
#pragma mark -- 加载数据的方法
            [weakSelf.ProductsArray removeAllObjects];
            [weakSelf shopCartDatasources];
            
            [weakSelf.ShopCartTableview reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    
    // 进入页面自动加载一次数据
    [refreshHeader autoRefreshWhenViewDidAppear];
    refreshHeader.refreshState = YES;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//添加购物车页面的UI
- (void)addShopCartUI {
    //完善导航栏上的内容
    [self addShopCartNavigationTitleUI];
    //添加tablebiew
    [self addShopCarttableview];
    
    //添加底部固定位置的工具条view
    [self addFootToolView];
    
}

- (void)addShopCartNavigationTitleUI {
    self.titleLabel.text = @"购物车";
    self.leftBtn.hidden = YES;
    
    messagelabel = [[UILabel alloc] initWithFrame:CGRectMake(WEITH -CGRectGetHeight(self.secondView.frame) - 10, 5, CGRectGetHeight(self.secondView.frame), CGRectGetHeight(self.secondView.frame)-10)];
    messagelabel.text = @"编辑";
    messagelabel.textColor = [UIColor blackColor];
    messagelabel.font = [UIFont systemFontOfSize:14];
    [self.secondView addSubview:messagelabel];
    
    messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    messageBtn.frame = CGRectMake(WEITH - CGRectGetHeight(self.secondView.frame), 0, CGRectGetHeight(self.secondView.frame), CGRectGetHeight(self.secondView.frame));
    [self.secondView addSubview:messageBtn];
    [messageBtn addTarget:self action:@selector(messageBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addShopCarttableview {
    self.ShopCartTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.8-CGRectGetHeight(self.tabBarController.tabBar.frame)+HIGHT*0.04) style:UITableViewStylePlain];
    self.ShopCartTableview.dataSource = self;
    self.ShopCartTableview.delegate = self;
    self.ShopCartTableview.backgroundColor = [UIColor clearColor];
    //去除空置的cell
    self.ShopCartTableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.ShopCartTableview];
}

//添加工具条
- (void)addFootToolView {
    
    ShopCartToolbar *toolbar = [[ShopCartToolbar alloc] initWithFrame:CGRectMake(0, HIGHT-CGRectGetHeight(self.tabBarController.tabBar.frame)-HIGHT*0.06, WEITH, HIGHT*0.06)];
    toolbar.delegate = self;
    
    self.toolBar = toolbar;
    [self.view addSubview:self.toolBar];
    //计算总价
    [self totalPrice];
    
}

- (void)messageBtnClick {
    NSLog(@"编辑按钮%ld", self.isEditing);
    // 切换编辑状态
    self.isEditing = !_isEditing;
    // 设置导航栏按钮
    [self configRightBarButtonItemWithTitle:_isEditing ? @"完成" : @"编辑"];
    // 切换cell全选状态
    [self setSelectAll:!_isEditing];
    // 刷新表格和工具条
    //    [self reloadData:YES fromFile:NO];
    [self.ShopCartTableview reloadData];
    NSLog(@"%ld", self.isEditing);
}
- (void)configRightBarButtonItemWithTitle:(NSString *)title {
    messagelabel.text = title;
}



//#pragma mark - 通知TabBar修改购物车数字
//- (void)postNotificationToTabBarVC {
//    [[NSNotificationCenter defaultCenter] postNotificationName:SCProductBuyCountDidChangeNotification object:nil];
//}

//#pragma mark - cell上的按钮点击事件:点击选中按钮
//- (void)tableViewCell:(ShopCartTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath didClickButtonAtIndex:(NSInteger)index {
//    [self switchSelectedStateForCell:cell AtIndexPath:indexPath];
//}
//#pragma mark 切换cell选中状态
//- (void)switchSelectedStateForCell:(ShopCartTableViewCell *)cell AtIndexPath:(NSIndexPath *)indexPath {
//    // 切换cell选中状态
//    NSString *cellState = [self.cellItems[indexPath.row] boolValue] ? SC_NO_string : SC_YES_string;
//    [self.cellItems replaceObjectAtIndex:indexPath.row withObject:cellState];
//    // 刷新表格和工具条
//
//}

#pragma mark -- ShopCartToolbarDelegate
- (void)shopCartToolBar:(UIView *)toolBar didClickButton:(UIButton *)button {
    NSLog(@"点解了%ld", button.tag);
    switch (button.tag) {
        case 0:
        {
            NSLog(@"点击了选择按钮");
            
            if (Alljudge == YES) {
                [button setImage:[UIImage imageNamed:@"flight_butn_check_unselect.png"] forState:UIControlStateNormal];
                Alljudge = NO;
                [self.allGoodsArrM removeAllObjects];
            } else {
                [button setImage:[UIImage imageNamed:@"flight_butn_check_select.png"] forState:UIControlStateNormal];
                Alljudge = YES;
                [self.allGoodsArrM addObjectsFromArray:self.ProductsArray];
            }
            //改变单元格选中状态
            for (int i=0; i<self.ProductsArray.count; i++)
            {   NSMutableDictionary *dicM = self.ProductsArray[i];
                
                dicM[@"selectState"] = [NSString stringWithFormat:@"%d", Alljudge];
                [self.ProductsArray replaceObjectAtIndex:i withObject:dicM];
                self.scpInfo = [[SCProductInfo alloc] initWithDict:[self.ProductsArray objectAtIndex:i]];
                self.scpInfo.selectState = Alljudge;
                //                [self.ProductsArray replaceObjectAtIndex:i withObject:self.scpInfo];
            }
            
            
            //计算价格
            [self totalPrice];
            //刷新表格
            [self.ShopCartTableview reloadData];
            
            
        }
            break;
        case 1:
        {
            NSLog(@"点击了第二个按钮");
        }
            break;
        case 2:
        {
            NSLog(@"点击了结算按钮");
            if (self.ProductsArray.count == 0 || self.allGoodsArrM.count == 0) {
                UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有商品" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alview show];
                NSLog(@"没有商品！！！");
                break;
            }
#pragma mark -- 传入数据
            
            ConfirmViewController *confirmVC = [[ConfirmViewController alloc] init];
            confirmVC.goodsInfoArrM = [NSMutableArray array];
            [self.allGoodsArrM removeAllObjects];
            for (int i = 0; i < self.ProductsArray.count; i ++) {
                self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[i]];
                if (self.scpInfo.selectState) {
                    if ([self.scpInfo.shopCartType isEqualToString:@"未选择"] || [self.scpInfo.shopcartColor isEqualToString:@"未选择"]) {
                        continue;
                    }
                    [self.allGoodsArrM addObject:self.ProductsArray[i]];
                    int feilv = [self.ProductsArray[i][@"feilv"] intValue];
                    int nums = [self.ProductsArray[i][@"nums"] intValue];
                    int jiage = [self.ProductsArray[i][@"jiage"] intValue];
                    float oneFeilv = jiage * nums * feilv * 0.01 * 1.0;
                    
                    confirmVC.allRate = confirmVC.allRate + oneFeilv;
                    
                    confirmVC.allYunFei = confirmVC.allYunFei + [self.ProductsArray[i][@"jzh"] intValue];
                }
            }
            [confirmVC.goodsInfoArrM addObjectsFromArray:self.allGoodsArrM];
            confirmVC.picNameNum = 0;
            confirmVC.allPrice = self.allGoodsPrice;
            confirmVC.wholeAddArray =  self.wholeAddArray;
            [self presentViewController:confirmVC animated:YES completion:nil];
            
        }
            break;
        default:
            break;
    }
}

- (void)addgetaddressdatasources {
    
    AFHTTPRequestOperationManager *mgra = [AFHTTPRequestOperationManager manager];
    mgra.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //    NSString *username = nameStr;
    
    
    
    NSDictionary *dic = [ReadLoginData readLoginData];
    NSString *userName = [dic[@"data"] objectForKey:@"username"];
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", userName]};
    
    [mgra POST:@"http://www.ylgw8.com/AppUser/Getdz" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
                self.wholeAddArray  = [self toArrayOrNSDictionary:responseObject];
        //
        [self.ShopCartTableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
     }];
}

#pragma mark -- UITableViewDataSource
//返回
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditing == NO) {
        
        ShopCartTableViewCell *cell = [ShopCartTableViewCell cellWithTableView:tableView];
        
        self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[indexPath.section]];
        
        [cell addTheValue:self.scpInfo];
        cell.delegate = self;
        
        return cell;
    } else {
        
        ShopCartEditTableViewCell *editCell = [ShopCartEditTableViewCell cellWithTableView:tableView];
        self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[indexPath.section]];
        [editCell addEditCellValue:self.scpInfo];
        editCell.delegate = self;
        return editCell;
    }

}
//返回分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"%ld", self.ProductsArray.count);
    return self.ProductsArray.count;
    
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 5;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    [self.ShopCartTableview.tableFooterView removeFromSuperview];
    
    UIView *footview =[[UIView alloc] init];
    footview.backgroundColor = [UIColor grayColor];
    //    footview.alpha = 0.5;
    return footview;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //判断, 当进入编辑模式的时候取消点击效果, 只有不在编辑状态的时候才可以进行点击后跳转到上平详情页面
    if (self.editing == NO) {
        
        
        ShoperDetailViewController *shopDetailVC = [[ShoperDetailViewController alloc] init];
        shopDetailVC.shopID = self.ProductsArray[indexPath.section][@"pid"];
        [self presentViewController:shopDetailVC animated:YES completion:nil];
    }
}
#pragma mark -- ShopCartTableViewCellDelegate
- (void)btnClick:(ShopCartTableViewCell *)cell button:(UIButton *)sender {
    NSIndexPath *index = [self.ShopCartTableview indexPathForCell:cell];
    
    self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[index.section]];
    
    self.scpInfo.selectState = !self.scpInfo.selectState;
    
    
    NSMutableDictionary *dicM = self.ProductsArray[index.section];
    dicM[@"selectState"] = [NSString stringWithFormat:@"%d", self.scpInfo.selectState];
    [self.ProductsArray replaceObjectAtIndex:index.section withObject:dicM];
    
    [self totalPrice];
    [self.ShopCartTableview reloadData];
    
    
    
    for (int i = 0; i < self.ProductsArray.count; i ++) {
        SCProductInfo *scproductInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[i]];
        if (scproductInfo.selectState) {
            continue;
        } else {
            Alljudge = NO;
            [self.toolBar.selectButton setImage:[UIImage imageNamed:@"flight_butn_check_unselect.png"] forState:UIControlStateNormal];
            NSLog(@"---------%d", Alljudge);
            return;
        }
    }
    Alljudge = YES;
    [self.toolBar.selectButton setImage:[UIImage imageNamed:@"flight_butn_check_select.png"] forState:UIControlStateNormal];
    
    NSLog(@"---------%d", Alljudge);
}

#pragma mark -- 计算价格
-(void)totalPrice
{
    //遍历整个数据源，然后判断如果是选中的商品，就计算价格（单价 * 商品数量）
    for ( int i =0; i<self.ProductsArray.count; i++)
    {
        self.scpInfo = [[SCProductInfo alloc] initWithDict:[self.ProductsArray objectAtIndex:i]];
        if (self.scpInfo.selectState)
        {
            allPrice = allPrice + [self.scpInfo.shopcartCount intValue] *[self.scpInfo.shopcartPrice intValue];
        }
    }
    
    //给总价文本赋值
    //    self.toolBar.totalView.priceLabel.text =[NSString stringWithFormat:@"¥ %.2f",allPrice];
    [self.toolBar.totalPriceView setTitle:[NSString stringWithFormat:@"¥ %.2f",allPrice] forState:UIControlStateNormal];
    
    NSLog(@"%f",allPrice);
    self.allGoodsPrice = allPrice;
    //每次算完要重置为0，因为每次的都是全部循环算一遍
    allPrice = 0.0;
}

//编辑cell的代理方法
#pragma mark -- ShopCartEditTableViewCellDelegate

- (void)editbtnClick:(UITableViewCell *)cell button:(UIButton *)sender {
    NSIndexPath *index = [self.ShopCartTableview indexPathForCell:cell];
    self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[index.section]];
    
    self.scpInfo.selectState = !self.scpInfo.selectState;
    
    NSMutableDictionary *dicM = self.ProductsArray[index.section];
    dicM[@"selectState"] = [NSString stringWithFormat:@"%d", self.scpInfo.selectState];
    [self.ProductsArray replaceObjectAtIndex:index.section withObject:dicM];
    [self.ShopCartTableview reloadData];
    [self totalPrice];
    
    for (int i = 0; i < self.ProductsArray.count; i ++) {
        SCProductInfo *scproductInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[i]];
        if (scproductInfo.selectState) {
            continue;
        } else {
            Alljudge = NO;
            [self.toolBar.selectButton setImage:[UIImage imageNamed:@"flight_butn_check_unselect.png"] forState:UIControlStateNormal];
            NSLog(@"---------%d", Alljudge);
            return;
        }
    }
    Alljudge = YES;
    [self.toolBar.selectButton setImage:[UIImage imageNamed:@"flight_butn_check_select.png"] forState:UIControlStateNormal];
    
    NSLog(@"---------%d", Alljudge);
}
-(void)deleteBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
    NSIndexPath *index = [self.ShopCartTableview indexPathForCell:cell];
    NSDictionary *dic = self.ProductsArray[index.section];
    [self.ProductsArray removeObjectAtIndex:index.section];
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *param = @{@"id":[NSString stringWithFormat:@"%@", dic[@"id"]]};
    
    [mgr GET:@"http://www.ylgw8.com/AppShopGoods/delGwc" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        NSLog(@"%@", [self toArrayOrNSDictionary:responseObject]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    [self.ShopCartTableview reloadData];
    [self totalPrice];
}

- (void)addBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
    NSIndexPath *index = [self.ShopCartTableview indexPathForCell:cell];
    //先获取到当期行数据源内容，改变数据源内容，刷新表格
    
    self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[index.section]];
    self.scpInfo.shopcartCount = [NSString stringWithFormat:@"%d", ([self.scpInfo.shopcartCount intValue]+1)];
    
    NSMutableDictionary *dicM = self.ProductsArray[index.section];
    dicM[@"nums"] = [NSString stringWithFormat:@"%@", self.scpInfo.shopcartCount];
    [self.ProductsArray replaceObjectAtIndex:index.section withObject:dicM];
    
    [self.ShopCartTableview reloadData];
    [self totalPrice];
}
- (void)reductionBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
    NSIndexPath *index = [self.ShopCartTableview indexPathForCell:cell];
    self.scpInfo = [[SCProductInfo alloc] initWithDict:self.ProductsArray[index.section]];
    
    if ([self.scpInfo.shopcartCount intValue] > 1){
        self.scpInfo.shopcartCount = [NSString stringWithFormat:@"%d", ([self.scpInfo.shopcartCount intValue]-1)];
    }
    NSMutableDictionary *dicM = self.ProductsArray[index.section];
    dicM[@"nums"] = [NSString stringWithFormat:@"%@", self.scpInfo.shopcartCount];
    [self.ProductsArray replaceObjectAtIndex:index.section withObject:dicM];
    
    [self.ShopCartTableview reloadData];
    [self totalPrice];
}

//首页精品推荐
- (void)shopCartDatasources {
    
    Alljudge = YES;
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic = [ReadLoginData readLoginData];
    
#warning --------用户名固定死数据
    NSString *userName = [dic[@"data"] objectForKey:@"username"];
    //
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", userName]};
    
    [mgr POST:@"http://www.ylgw8.com/AppShopOrder/GwcList" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dicM = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSArray *arrayM = dicM[@"data"];
        if ([dicM[@"status"] intValue] == 0) {
            return;
        }
        for (NSDictionary *dic in arrayM) {
            //            [dic setValue:@"YES" forKey:@"selectState"];
            NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
            [dic1 setValue:@"YES" forKey:@"selectState"];
            
            [dic1 addEntriesFromDictionary:dic];
            NSLog(@"%@", dic1);
            [self.ProductsArray addObject:dic1];
        }
        [self.allGoodsArrM addObjectsFromArray:self.ProductsArray];
        
        [self.ShopCartTableview reloadData];
        [self totalPrice];
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
         NSLog(@"数据请求错误原因:%@", error);
         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
